import 'https://cdn.bootcdn.net/ajax/libs/jquery/2.2.0/jquery.js';
class Fangda {
    constructor() {
        this.scale = $('.scale'); //包含框
        this.spic = $('.spic'); //小图
        this.sf = $('.sf'); //小放
        this.bf = $('.bf'); //大放
        this.bpic = $('.bpic'); //大图
    }

    init() {
        // 1.鼠标移入小图，显示隐藏小放和大放。
        this.spic.hover(() => {
            this.sf.css('visibility', 'visible');
            this.bf.css('visibility', 'visible');
            // this.bf.css('display', 'block');



            this.sf.width(this.spic.width() * this.bf.width() / this.bpic.width());
            this.sf.height(this.spic.height() * this.bf.height() / this.bpic.height());


            this.bili = this.bpic.width() / this.spic.width();


            this.spic.on('mousemove', (e) => {
                let leftvalue = e.pageX - this.scale.offset().left - this.sf.width() / 2;
                let topvalue = e.pageY - this.scale.offset().top - this.sf.height() / 2;

                if (leftvalue < 0) {
                    leftvalue = 0;
                } else if (leftvalue > this.spic.width() - this.sf.width()) {
                    leftvalue = this.spic.width() - this.sf.width();
                }
                if (topvalue < 0) {
                    topvalue = 0;
                } else if (topvalue > this.spic.height() - this.sf.height()) {
                    topvalue = this.spic.height() - this.sf.height();
                }
                // 小放移动
                this.sf.css({
                    left: leftvalue,
                    top: topvalue
                })

                // 大图移动
                this.bpic.css({
                    left: -leftvalue * this.bili,
                    top: -topvalue * this.bili
                });
            })

        }, () => {
            this.sf.css('visibility', 'hidden');
            this.bf.css('visibility', 'hidden');
            // this.bf.css('display', 'none');

        });


        //加入购物车
        let detailObj = {};

        if (localStorage.getItem('goods')) {
            detailObj = JSON.parse(localStorage.getItem('goods'));
        } else {
            detailObj = {}
        }

        $('.main-xq .p-btn a').on('click', function() {
            if (Object.keys(detailObj).includes('product_' + sid)) {
                detailObj['product_' + sid] = parseInt(detailObj['product_' + sid]) + parseInt($('.p-btn #count').val());
            } else {
                detailObj['product_' + sid] = $('.p-btn #count').val();

            }
            localStorage.setItem('goods', JSON.stringify(detailObj));
        })


    }

}
export {
    Fangda
}